forEach
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div class="app">
<h1>即時消息</h1>
<ul class="list"></ul>
<a href="#">More</a>
</div>
<script>
const title = [
"這是一條假消息 1",
"這是一條假消息 2",
"這是一條假消息 3",
"這是一條假消息 4",
];
let html = "";
function renderDom() {
title.forEach((item) => {
console.log(item);
html += `<li><a href="#">${item}</a></li>`;
});
}
renderDom();
document.querySelector(".list").innerHTML = html;
</script>
</body>
</html>